package cn.hinglo.common.generator;

import com.jfinal.kit.PathKit;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.generator.*;
import com.jfinal.plugin.hikaricp.HikariCpPlugin;

import javax.sql.DataSource;
import java.util.List;

/**
 * @createTime: 2018/2/11
 * @author: HingLo
 * @description: 工具
 */
public class DatabaseToBeanGenerator {
    public static HikariCpPlugin createDruidPlugin() {
        return new HikariCpPlugin(PropKit.get("jdbcUrl"), PropKit.get("user"), PropKit.get("password").trim());
    }

    public static DataSource getDataSource() {
        PropKit.use("application.properties");
        HikariCpPlugin druidPlugin = createDruidPlugin();
        druidPlugin.start();
        return druidPlugin.getDataSource();
    }

    public static void main(String[] args) {
        test1("core_api", "core", "permission");
    }

    /***
     * 创建指定table
     * @param tableName
     * @param removePre
     */
    public static void test1(String tableName, String removePre, String model) {


        String baseModelPackageName = "cn.hinglo." + model + ".model.bean";
        // base entity 文件保存路径
        String baseModelOutputDir = PathKit.getWebRootPath() + "/src/main/java/cn/hinglo/" + model + "/model/bean";
        // entity 所使用的包名 (MappingKit 默认使用的包名)
        String modelPackageName = "cn.hinglo." + model + ".model";
        // entity 文件保存路径 (MappingKit 与 DataDictionary 文件默认保存路径)
        String modelOutputDir = PathKit.getWebRootPath() + "/src/main/java/cn/hinglo/" + model + "/model";

        MetaBuilder metaBuilder = new MetaBuilder(getDataSource());
        metaBuilder.setGenerateRemarks(true);
        BaseModelGenerator baseModelGenerator = new BaseModelGenerator(baseModelPackageName, baseModelOutputDir);
        baseModelGenerator.setGenerateChainSetter(true);
        baseModelGenerator.setTemplate("/cn/hinglo/common/generator/base_model_template.jf");

        ModelGenerator modelGenerator = new ModelGenerator(modelPackageName, baseModelPackageName, modelOutputDir);
        modelGenerator.setGenerateDaoInModel(true);
        modelGenerator.setTemplate("/cn/hinglo/common/generator/model_template.jf");
        modelGenerator.setGenerateDaoInModel(true);

        metaBuilder.setRemovedTableNamePrefixes(removePre);
        List<TableMeta> tableMetas = metaBuilder.build();
        tableMetas.removeIf(action -> !action.name.equalsIgnoreCase(tableName));
        baseModelGenerator.generate(tableMetas);
        modelGenerator.generate(tableMetas);
    }

    /***
     * 创建all
     */
    public static void crateAll() {
        // base entity 所使用的包名
        String baseModelPackageName = "cn.hinglo.permission.model.bean";
        // base entity 文件保存路径
        String baseModelOutputDir = PathKit.getWebRootPath() + "/src/main/java/cn/hinglo/permission/model/bean";

        // entity 所使用的包名 (MappingKit 默认使用的包名)
        String modelPackageName = "cn.hinglo.permission.model";
        // entity 文件保存路径 (MappingKit 与 DataDictionary 文件默认保存路径)
        String modelOutputDir = PathKit.getWebRootPath() + "/src/main/java/cn/hinglo/permission/model";

        // 创建生成器
        Generator generator = new Generator(getDataSource(), baseModelPackageName, baseModelOutputDir, modelPackageName, modelOutputDir);
        // 生产备注信息
        generator.setGenerateRemarks(true);
        // 设置是否生成链式 setter 方法
        generator.setGenerateChainSetter(true);
        // 设置是否在 Model 中生成 DAO 对象
        generator.setGenerateDaoInModel(true);
        // 设置是否生成字典文件
        generator.setGenerateDataDictionary(false);
        // 设置需要被移除的表名前缀用于生成modelName。例如表名 "osc_user"，移除前缀 "osc_"后生成的model名为 "User"而非 OscUser
        generator.setRemovedTableNamePrefixes("sys_", "core_", "bs_");
        generator.setMappingKitClassName("MappingKit");
        // 生成
        generator.generate();
    }


}
